﻿define(function (require) {
    var datacontext = require('modules/dataservice');
    var helpers = require('modules/helpers');
    var hubService = require('modules/hubService');
    var app = require('durandal/app');
    var system = require('durandal/system');

    function extendVm() {
        vm.orderLinesSelectedCount = ko.computed(function () {
            var selected = 0;
            if (vm.order()) {
                $.each(vm.order().OrderLines(), function (index, orderLine) {
                    if (orderLine.isSelected && orderLine.isSelected())
                        selected++;
                });
            }
            return selected;
        });
    }

    var vm = {
        order: ko.observable(),
        activate: function () {
            var self = this;
            extendVm();
            hubService.connectToOrderView(self.order().ReferenceId());
        },
        deactivate: function () {
            var self = this;
            hubService.disconnectFromOrderView(self.order().ReferenceId());
        },
        canActivate: function (params) {
            var self = this;
            if (params && params > 0) {
                return datacontext.getOrder(params, function (dbOrder) {
                    $.each(dbOrder.OrderLines(), function (ind, ol) {
                        ol.isSelected = ko.observable(false);
                    });
                }).then(function (dbOrder) {
                    self.order(dbOrder);
                    return true;
                });
            }
            else {
                var order = datacontext.createOrder({ CreationDate: new Date(), ReferenceId: helpers.generateGuid(), IsActive: true, IsPending: true });
                return app.showDialog('viewmodels/order/customerSelection', order).then(function (result) {
                    if (result) {
                        datacontext.addEntity(order);
                        datacontext.saveChanges();
                        datacontext.extendOrder(order);
                        self.order(order);
                        return true;
                    }
                    else {
                        order.entityAspect.rejectChanges();
                        return false;
                    }
                });
            }
        },
        changeStatus: function () {
            var self = this;
            self.order().IsPending(!self.order().IsPending());
        },
        addOrderLine: function () {
            var self = this;
            app.showDialog('viewmodels/order/orderLineSelection', self.order).then(function (result) {
                if (result.success) {
                    self.order().OrderLines.push(result.orderLine());
                    self.order().entityAspect.setModified();
                    datacontext.saveChanges();
                }
                return true;
            });
        },
        removeOrderLines: function () {
            var self = this;
            var deletedItems = [];
            $.each(self.order().OrderLines(), function (index, orderLine) {
                if (orderLine && orderLine.isSelected()) {
                    deletedItems.push(orderLine);
                }
            });
            $.each(deletedItems, function () {
                this.entityAspect.setDeleted();
            });
            datacontext.saveChanges();

        }
    };

    return vm;
});